﻿
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;

namespace CommonTagTime.DataAccess
{
    public class daoUserProfile
    {
        public daoUserProfile()
        {

        }
        private UserProfile Load(IDataReader de)
        {
            UserProfile item = new UserProfile();
            //item.CategoryID = clsConvert.Int(clsString.CheckObject(de["CategoryID"]));
            item.UserId = clsConvert.DbToGuid("UserId", de);
            item.FirstName = clsConvert.DbToString("FirstName", de);
            item.LastName = clsConvert.DbToString("LastName", de);
            item.UserCode = clsConvert.DbToString("UserCode", de);
            item.IsAllowLogin = clsConvert.DbToBoolean("IsAllowLogin", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.ModifiedBy = clsConvert.DbToString("ModifiedBy", de);
            item.ModifiedDate = clsConvert.DbToDateTime("ModifiedDate", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }



        public UserProfile GetByID(Guid UserId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            UserProfile item =null;

            try
            {
                para[0] = new SqlParameter(busUserProfile.UserId, SqlDbType.UniqueIdentifier);
                para[0].Value = UserId;
                using (de = clsSQL.SPDataReader("UserProfile_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoUserProfile", "GetByID(Guid UserId)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }





        public bool Insert(UserProfile objData)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[7];
            try
            {
               
                para[0] = new SqlParameter(busUserProfile.UserId, SqlDbType.UniqueIdentifier);
                para[0].Value = objData.UserId;
                para[1] = new SqlParameter(busUserProfile.FirstName, SqlDbType.NVarChar);
                para[1].Value = clsConvert.StringToDB(objData.FirstName);
                para[2] = new SqlParameter(busUserProfile.LastName, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objData.LastName);
                para[3] = new SqlParameter(busUserProfile.UserCode, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objData.UserCode);
                para[4] = new SqlParameter(busUserProfile.IsAllowLogin, SqlDbType.Bit);
                para[4].Value = objData.IsAllowLogin;
                para[5] = new SqlParameter(busUserProfile.CreatedBy, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objData.CreatedBy);
                para[6] = new SqlParameter(busUserProfile.CreatedDate, SqlDbType.DateTime);
                para[6].Value = clsConvert.DateToDB(objData.CreatedDate);
                result = clsSQL.SPExecute("UserProfile_Insert", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoUserProfile", "Insert(UserProfile objData)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool Update(UserProfile objData)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[7];
            try
            {
                para[0] = new SqlParameter(busUserProfile.UserId, SqlDbType.UniqueIdentifier);
                para[0].Value = objData.UserId;
                para[1] = new SqlParameter(busUserProfile.FirstName, SqlDbType.NVarChar);
                para[1].Value = clsConvert.StringToDB(objData.FirstName);
                para[2] = new SqlParameter(busUserProfile.LastName, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objData.LastName);
                para[3] = new SqlParameter(busUserProfile.UserCode, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objData.UserCode);
                para[4] = new SqlParameter(busUserProfile.IsAllowLogin, SqlDbType.Bit);
                para[4].Value = objData.IsAllowLogin;
                para[5] = new SqlParameter(busUserProfile.ModifiedBy, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objData.ModifiedBy);
                para[6] = new SqlParameter(busUserProfile.ModifiedDate, SqlDbType.DateTime);
                para[6].Value = clsConvert.DateToDB(objData.ModifiedDate);

                result = clsSQL.SPExecute("UserProfile_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoUserProfile", "Update(UserProfile objData)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
         public UserProfileCollection List(UserProfile objUserProfile)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            UserProfileCollection items = new UserProfileCollection();

            try
            {
                para[0] = new SqlParameter(busUserProfile.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objUserProfile.OrderBy;
                para[1] = new SqlParameter(busUserProfile.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objUserProfile.OrderDirection;
                para[2] = new SqlParameter(busUserProfile.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objUserProfile.CurrentPage;
                para[3] = new SqlParameter(busUserProfile.PAGESIZE, SqlDbType.Int);
                para[3].Value = objUserProfile.PageSize;
                para[4] = new SqlParameter(busUserProfile.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objUserProfile.Condition;
                para[5] = new SqlParameter(busUserProfile.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("UserProfile_List", para);
                while (de.Read())
                {
                    UserProfile item = Load(de);
                    item.UserId = clsConvert.DbToGuid("UserId", de);
                    item.FirstName = clsConvert.DbToString("FirstName", de);
                    item.LastName = clsConvert.DbToString("LastName", de);
                    item.UserCode = clsConvert.DbToString("UserCode", de);
                    item.IsAllowLogin = clsConvert.DbToBoolean("IsAllowLogin", de);
                    item.UserName = clsConvert.DbToString("UserName", de);
                    item.Email = clsConvert.DbToString("Email", de);
                    items.Add(item);
                }

                de.Close();
                objUserProfile.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoUserProfile", "List(UserProfile objUserProfile)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
         public bool Delete(Guid userId)
         {
             int result = 0;
             clsSQL clsSQL = new clsSQL();
             SqlParameter[] para = new SqlParameter[1];
             try
             {
                 para[0] = new SqlParameter(busUserProfile.UserId, SqlDbType.UniqueIdentifier);
                 para[0].Value = userId;
                 result = clsSQL.SPExecute("UserProfile_Delete", para);
             }
             catch (Exception ex)
             {
                 clsFile.WriteLog("daoUserProfile", "Delete(Guid userId)", ex.ToString());
             }
             finally
             {
                 clsSQL.Dispose();
                 clsSQL = null;
             }
             if (result > 0)
                 return true;
             return false;
         }
    }
}
